package org.bigk.invoices.actions.purchasers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bigk.invoices.actions.AbstractCRUDAction;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.Purchaser;
import org.bigk.invoices.model.PurchaserFilter;
import org.bigk.invoices.services.PurchasersService;
import org.bigk.invoices.utils.DisplayTagUtils;


public class PurchasersAction extends AbstractCRUDAction<Purchaser> {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(PurchasersAction.class);

	private static final long serialVersionUID = 3190832719110023997L;
	
	private Long id;
	private PurchaserFilter filter;
	private PurchasersService purchasersService;

	@Override
	public void prepareList() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("prepareList() - start");
			logger.debug("prepareList() - this.filter=" + this.filter);
		}
		
		if (this.filter == null)
			this.filter = new PurchaserFilter();
		
		this.filter.setCurrentPage(DisplayTagUtils.getCurrentPage4Id("rs"));
		
		list = purchasersService.listItems4Page(this.filter);

		if (logger.isDebugEnabled()) {
			logger.debug("prepareList() - filter=" + filter);
			logger.debug("prepareList() - end");
		}
	}
	
	@Override
	public void prepareShow() throws ServiceException {
		model = purchasersService.getPurchaser(this.getId());
	}
	
	@Override
	public void prepareEdit() throws ServiceException {
		model = purchasersService.getPurchaser(this.getId());
	}

	@Override
	public void prepareSave() throws ServiceException {
		model = new Purchaser();
	}

	@Override
	public String save() throws ServiceException {
		purchasersService.savePurchaser(model);
		addActionMessage(getText("msg.recordInsertedSuccess"));
		return REDIRECT_LIST;
	}
	
	@Override
	public void prepareUpdate() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("prepareUpdate() - start");
		}

		model = purchasersService.getPurchaser(id);

		if (logger.isDebugEnabled()) {
			logger.debug("prepareUpdate() - end");
		}
	}
	
	@Override
	public String update() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("update() - start");
		}

		purchasersService.updatePurchaser(model);
		addActionMessage(getText("msg.recordUpdatedSuccess"));

		if (logger.isDebugEnabled()) {
			logger.debug("update() - end - return value=" + REDIRECT_LIST);
		}
		return REDIRECT_LIST;
	}

	@Override
	public void prepareDelete() throws ServiceException {
		model = purchasersService.getPurchaser(id);
	}
	
	@Override
	public String delete() throws ServiceException {
		purchasersService.deletePurchaser(model);
		return REDIRECT_LIST;
	}
	
	public PurchaserFilter getFilter() {
		return filter;
	}

	public void setFilter(PurchaserFilter filter) {
		this.filter = filter;
	}

	public PurchasersService getPurchasersService() {
		return purchasersService;
	}

	public void setPurchasersService(PurchasersService purchasersService) {
		this.purchasersService = purchasersService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
